package com.planet.apiimpl.service;

import com.planet.api.domain.BatchSendRequest;
import com.planet.api.domain.SendRequest;
import com.planet.api.domain.SendResponse;
import com.planet.api.service.SendService;
import com.planet.apiimpl.domain.SendTaskModel;
import com.planet.common.vo.BasicResultVO;
import com.planet.support.pipeline.ProcessContext;
import com.planet.support.pipeline.ProcessController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;


@Service
public class SendServiceImpl implements SendService {

    @Autowired
    private ProcessController processController;
    //单发
    @Override
    public SendResponse send(SendRequest sendRequest) {
        // 1.获取上下文
        SendTaskModel sendTaskModel = SendTaskModel.builder()
                .messageTemplateId(sendRequest.getMessageTemplateId())
                .messageParamList(Arrays.asList(sendRequest.getMessageParam()))
                .build();

        // 4.把上下文封装到processModel里面 得到一个大的上下文 然后把上下文放到我们的责任链执行器去执行
        ProcessContext context = ProcessContext.builder()
                // 2.得到接口入参的责任链标识
                .code(sendRequest.getCode())
                // 3.把上下文封装在processModel里面
                .processModel(sendTaskModel)
                .needBreak(false)
                .response(BasicResultVO.success())
                .build();
        // 5.把上下文放在我们责任链执行器上去执行
        ProcessContext process = processController.process(context);

        return new SendResponse(process.getResponse().getCode(),process.getResponse().getMsg());
    }
    //群发
    @Override
    public SendResponse batchSend(BatchSendRequest batchSendRequest) {
        SendTaskModel  sendTaskModel = SendTaskModel.builder()
                .messageTemplateId(batchSendRequest.getMessageTemplateId())
                .messageParamList(batchSendRequest.getMessageParamList())
                .build();
        ProcessContext context = ProcessContext.builder()
                .code(batchSendRequest.getCode())
                .processModel(sendTaskModel)
                .needBreak(false)
                .response(BasicResultVO.success()).build();
        ProcessContext process = processController.process(context);
        return new SendResponse(process.getResponse().getCode(), process.getResponse().getMsg());
    }


}
